package site.wetsion.galio.sdk.utils;

import site.wetsion.galio.sdk.common.GalioOauthAccessToken;
import site.wetsion.galio.sdk.common.UserDetails;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 系统中accessToken的缓存，key为token字符串，value为token对象
 *
 * @author weixin
 * @version 1.0
 * @CLassName AccessTokenHolder
 * @date 2019/4/28 4:49 PM
 */
public class AccessTokenHolder {

    private static final ConcurrentHashMap<String, GalioOauthAccessToken> TOKEN_MAP =
            new ConcurrentHashMap<>();

    private static final ConcurrentHashMap<String, UserDetails> TOKEN_USER_MAP =
            new ConcurrentHashMap<>();

    public static GalioOauthAccessToken get(String key) {
        if (key == null) {
            return null;
        }
        return TOKEN_MAP.get(key);
    }

    public static synchronized void save(String key, GalioOauthAccessToken value) {
        TOKEN_MAP.put(key, value);
    }

    public static synchronized GalioOauthAccessToken remove(String key) {
        return TOKEN_MAP.remove(key);
    }

    public static UserDetails getUserByToken(String key) {
        if (key == null) {
            return null;
        }
        return TOKEN_USER_MAP.get(key);
    }

    public static synchronized void saveUser(String key, UserDetails value) {
        TOKEN_USER_MAP.put(key, value);
    }

    public static synchronized UserDetails removeUser(String key) {
        return TOKEN_USER_MAP.remove(key);
    }
}
